import vue from '@vitejs/plugin-vue'
import path from 'path'
import UnoCSS from 'unocss/vite'
import { defineConfig, loadEnv, UserConfig } from 'vite'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
// import AutoImport from 'unplugin-auto-import/vite'
// import Components from 'unplugin-vue-components/vite'
// import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default ({ mode }: UserConfig) =>
  defineConfig({
    plugins: [
      vue(),
      UnoCSS(),
      // AutoImport({
      //   resolvers: [ElementPlusResolver()],
      // }),
      // Components({
      //   resolvers: [ElementPlusResolver()],
      // }),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]',
      }),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '#': path.join(__dirname, './types'),
        '&': path.join(__dirname, './src/views/backend'),
        '&page': path.join(__dirname, './src/views/backend/pages'),
      },
    },
    server: {
      open: true,
      proxy: {
        '/api': {
          // target: 'http://192.168.3.234:80',
          target: 'http://47.92.235.160:8091',
          changeOrigin: true,
        },
        '/file': {
          target: 'http://47.92.235.160:8090/file',
          changeOrigin: true,
        },
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "./src/styles/var.scss";
                      @import "./src/styles/mixins.scss";`,
        },
      },
    },
    esbuild: {
      pure: loadEnv(mode as string, process.cwd()).VITE_DROP_CONSOLE
        ? ['console.log', 'debugger']
        : [],
      supported: {
        // https://github.com/vitejs/vite/pull/8665
        'top-level-await': true,
      },
    },
    build: {
      // sourcemap: true,
      target: 'esnext',
      manifest: true,
      rollupOptions: {
        output: {
          manualChunks: {
            vue: ['vue', 'vue-router', 'pinia'],
            'element-plus': ['element-plus'],
            // echarts: ['echarts'],
            'vue-i18n': ['vue-i18n'],
            'lodash-es': ['lodash-es'],
          },
          chunkFileNames: 'assets/js/[name]-[hash].js',
          entryFileNames: 'assets/js/[name]-[hash].js',
          assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
        },
      },
      chunkSizeWarningLimit: 1500,
    },
  })
